package com.han.hotelplat.cms.controller.datacenter.finance;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.utils.HssfUtil;
import com.han.hotelplat.service.datacenter.finance.FinanceService;

/**
 * 
	 ***************************************************************
	 * <p>
	 * @CLASS				:	CMSFinanceController
	 * @DESCRIPTION	:	
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @DATE					:	2018年10月29日 下午5:35:44            
	 * </p>
	 * 
	 * <p>
	 * @MODIFY	LOG	:		
	 * @AUTHOR				:	han1088
	 * @VERSION			:	v1.0
	 * @MODIFY DATE	:	
	 * </p>    
	 ****************************************************************
 */
@Controller
@RequestMapping("/cms/datacenter/finance")
public class CMSFinanceController {
	@Autowired
	private FinanceService financeService;
	
	/**
	 * 获取收支数据按照考勤审核结果统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getFinanceTotalGroupResult", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getFinanceTotalGroupResult(String startTime, String endTime, Integer type){
		Map<String,Object> data = financeService.getFinanceTotalGroupResult(startTime, endTime, type);
		return new ResponseWrapTo((int)data.get("result"), data.get("msg").toString(), data).toJson();
	}
	
	/**
	 * 获取酒店支出数据按照区域统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getFinanceHotelCostGroupArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getFinanceHotelCostGroupArea(String startTime, String endTime, Integer type, Integer groupArea){
		Map<String,Object> data = financeService.getFinanceHotelCostGroupAreaResult(startTime, endTime, type, groupArea);
		return new ResponseWrapTo((int)data.get("result"), data.get("msg").toString(), data).toJson();
	}
	
	/**
	 * 获取帮工收入数据按照区域统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getFinanceEmRewardGroupArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getFinanceEmRewardGroupArea(String startTime, String endTime, Integer type, Integer groupArea){
		Map<String,Object> data = financeService.getFinanceEmRewardGroupAreaResult(startTime, endTime, type, groupArea);
		return new ResponseWrapTo((int)data.get("result"), data.get("msg").toString(), data).toJson();
	}
	
	
	/**
	 * 获取酒店支出数据按照日期统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getFinanceEmRewardGroupDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getFinanceEmRewardGroupDate(String startTime, String endTime, Integer type, Integer areaCode){
		Map<String,Object> data = financeService.getFinanceEmRewardGroupDateResult(startTime, endTime, type, areaCode);
		return new ResponseWrapTo((int)data.get("result"), data.get("msg").toString(), data).toJson();
	}
	
	
	/**
	 * 获取酒店支出数据按照日期统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getFinanceHotelCostGroupDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public String getFinanceHotelCostGroupDate(String startTime, String endTime, Integer type,Integer areaCode){
		Map<String,Object> data = financeService.getFinanceHotelCostGroupDateResult(startTime, endTime, type, areaCode);
		return new ResponseWrapTo((int)data.get("result"), data.get("msg").toString(), data).toJson();
	}
	
	/**
	 * 导出酒店支出数据按照日期统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "exportFinanceHotelCostGroupDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public void exportFinanceHotelCostGroupDate(HttpServletResponse resp, HttpServletRequest req, String startTime, String endTime, Integer type,Integer areaCode){
		Map<String,Object> re = financeService.getFinanceHotelCostGroupDateResult(startTime, endTime, type, areaCode);
		List<JSONObject> data = (List<JSONObject>) re.get("hotelCostData");
		String filename = "酒店收支数据.xls";
		resp.setHeader("Content-Type", "application/octet-stream");
		try {
			resp.setHeader("Content-Disposition", "attachment;filename=" + HssfUtil.buildAttachmentFileName(req, filename));
			List<Object[]> datas = new ArrayList<Object[]>();
			String[] columnNames = new String[] { "日期","提交房量","确认房量","预计支出","实际支出"};
			for(JSONObject d : data){
				Object[] dataObj = new Object[columnNames.length];
				dataObj[0] = d.get("date");
				dataObj[1] = d.get("advanceRoomCount");
				dataObj[2] = d.get("realRoomCount");
				dataObj[3] = d.get("advanceCostMoney");
				dataObj[4] = d.get("realCostMoney");
				datas.add(dataObj);
			}
			HssfUtil.writeHssf("酒店收支数据", columnNames, datas, filename, resp.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 导出帮工收入数据按照日期统计
		 * @description	
		 * @author  han1088
		 * @param startTime
		 * @param endTime
		 * @param type
		 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "exportFinanceEmRewardGroupDate", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET, RequestMethod.POST })
	public void exportFinanceEmRewardGroupDate(HttpServletResponse resp, HttpServletRequest req, String startTime, String endTime, Integer type,Integer areaCode){
		Map<String,Object> re = financeService.getFinanceEmRewardGroupDateResult(startTime, endTime, type, areaCode);
		List<JSONObject> data = (List<JSONObject>) re.get("emRewardData");
		String filename = "帮工收入数据.xls";
		resp.setHeader("Content-Type", "application/octet-stream");
		try {
			resp.setHeader("Content-Disposition", "attachment;filename=" + HssfUtil.buildAttachmentFileName(req, filename));
			List<Object[]> datas = new ArrayList<Object[]>();
			String[] columnNames = new String[] { "日期","提交房量","确认房量","预计收入","实际收入"};
			for(JSONObject d : data){
				Object[] dataObj = new Object[columnNames.length];
				dataObj[0] = d.get("date");
				dataObj[1] = d.get("advanceRoomCount");
				dataObj[2] = d.get("realRoomCount");
				dataObj[3] = d.get("advanceRewardMoney");
				dataObj[4] = d.get("realRewardMoney");
				datas.add(dataObj);
			}
			HssfUtil.writeHssf("帮工收入数据", columnNames, datas, filename, resp.getOutputStream());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
